<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org">
<head>

<meta charset="utf-8"></meta>
<meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>


<title>添加</title>

<link rel="shortcut icon" href="favicon.ico"></link>
<link th:href="@{/css/bootstrap.min14ed.css}"
	href="css/bootstrap.min14ed.css" rel="stylesheet"></link>
<link th:href="@{/css/font-awesome.min93e3.css}"
	href="css/font-awesome.min93e3.css" rel="stylesheet"></link>
<link th:href="@{/css/plugins/iCheck/custom.css}"
	href="css/plugins/iCheck/custom.css" rel="stylesheet"></link>
<link th:href="@{/css/animate.min.css}" href="css/animate.min.css"
	rel="stylesheet"></link>
<link th:href="@{/css/style.min862f.css}" href="css/style.min862f.css"
	rel="stylesheet"></link>

<link th:href="@{/css/layui.css}" href="css/layui.css" rel="stylesheet"></link>
</head>

<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight"
		style="width: 100%; ">
		<input id="infoId" type="hidden" th:value="${info.id}"></input>


		<div class="row">
			<div class="col-sm-12">
				<div class="ibox float-e-margins">
					<div class="ibox-title">
						<h3>
							新建文章 <small> 请正确选择添加类型</small>
						</h3>

					</div>
					<div class="ibox-content">
						<form method="get" class="form-horizontal">
			
							<div class="form-group">
								<label class="col-sm-2 control-label">标题</label>

								<div class="col-sm-4">
									<input id="title" type="text" class="form-control"
										th:value="${info.title}"></input>
								</div>
								<div class="col-sm-2">
									<span class="help-block m-b-none"><e style="color:red">*</e>必填！</span>
								</div>
							</div>
							<div class="hr-line-dashed"></div>
							<div class="form-group">
								<label class="col-sm-2 control-label">添加类型</label>
								<div class="col-sm-4">
									<select id="type" class="form-control m-b" name="account">
										<option value="">--请选择--</option>

										<option th:each="type : ${typeList}" th:value="${type.id}"
											th:text="${type.typeName}"
											th:selected="${info.type==type.id or stype==type.id}"></option>
									</select> 
								</div>
								<div class="col-sm-2">
									<span class="help-block m-b-none"><e style="color:red">*</e>必填！</span>
								</div>
							</div>
							<div  id="type3">
							<div class="hr-line-dashed"></div>
							<div class="form-group">
								<label class="col-sm-2 control-label">企业名称</label>
								<div class="col-sm-4">
									<input id="companyName" type="text" class="form-control"
										th:value="${info.companyName}"></input>
								</div>
								
								<div class="col-sm-2">
									<span class="help-block m-b-none"><e style="color:red">*</e>必填！</span>
								</div>
							</div>
							<div class="hr-line-dashed"></div>
							<div class="form-group">
								<label class="col-sm-2 control-label">金额(元)</label>
								<div class="col-sm-4">
									<input id="amount" type="number" class="form-control"
										th:value="${info.amount}"></input>
								</div>
								
								<div class="col-sm-2">
									<span class="help-block m-b-none"><e style="color:red">*</e>必填！</span>
								</div>
							</div>
							<div class="hr-line-dashed"></div>
							<div class="form-group">
								<label class="col-sm-2 control-label">报告编码</label>
								<div class="col-sm-4">
									<input id="reportNum" type="text" class="form-control"
										th:value="${info.reportNum}"></input>
								</div>
								<div class="col-sm-2">
									<span class="help-block m-b-none"><e style="color:red">*</e>必填！</span>
								</div>
							</div>
							<div class="hr-line-dashed"></div>
							<div class="form-group">
								<label class="col-sm-2 control-label">报告密码</label>
								<div class="col-sm-4">
									<input id="reportCode" type="text" class="form-control"
										th:value="${info.reportCode}" disabled="disabled"></input>
								</div>
								<div class="col-sm-2">
									<span class="help-block m-b-none"><e style="color:red">*</e>必填！</span>
								</div>
							</div>
							</div>
							<div class="hr-line-dashed"></div>
							<div class="form-group">
								<label class="col-sm-2 control-label">添加时间</label>
								<div class="col-sm-4">
									<input id="hello" class=" form-control layer-date" placeholder="yyyy-MM-dd HH:mm:ss" 
									th:value="${createdAt}"></input>
								</div>
								<div class="col-sm-2">
									<span class="help-block m-b-none"><e style="color:red">*</e>必填！</span>
								</div>
							</div>
							<div class="hr-line-dashed"></div>
							<div class="form-group">
								<label class="col-sm-2 control-label">上传附件</label>
								<div class="col-sm-10">
									<div class="layui-upload">
										<button type="button" class="layui-btn layui-btn-normal"
											id="test8">选择文件</button>
										<button type="button" class="layui-btn" id="test9">开始上传</button>
										<span class="layui-inline layui-upload-choose" th:title="${info.file}" title="" th:text="${info.fileName
}" ></span>
										<div class="spiner-example"
											style="display: none; width: 50px; height: 30px; position: absolute; top: 10px; padding-top: 0;">
											<div class="sk-spinner sk-spinner-fading-circle">
												<div class="sk-circle1 sk-circle"></div>
												<div class="sk-circle2 sk-circle"></div>
												<div class="sk-circle3 sk-circle"></div>
												<div class="sk-circle4 sk-circle"></div>
												<div class="sk-circle5 sk-circle"></div>
												<div class="sk-circle6 sk-circle"></div>
												<div class="sk-circle7 sk-circle"></div>
												<div class="sk-circle8 sk-circle"></div>
												<div class="sk-circle9 sk-circle"></div>
												<div class="sk-circle10 sk-circle"></div>
												<div class="sk-circle11 sk-circle"></div>
												<div class="sk-circle12 sk-circle"></div>
											</div>
										</div>
									</div>
								</div>
							</div>
							<div>
							<div id="type4">
								<div class="hr-line-dashed"></div>
								<div class="form-group">
									<label class="col-sm-2 control-label">内容</label>
									<div class="col-sm-8">
										<!-- <textarea id="editor" placeholder="这里输入内容" autofocus
											>
				                        </textarea> -->
				                        <div id="editor" >
									        <p>欢迎使用 <b>wangEditor</b> 富文本编辑器</p>
									    </div>
									</div>
								</div>
							</div>
							</div>	
							<input id="filePath" type="hidden"></input>
							<input id="fileName" type="hidden"></input>

							<div class="hr-line-dashed"></div>
							<div class="form-group">
								<div class="col-sm-4 col-sm-offset-2">
									<button class="btn btn-primary" type="submit" onclick="save();return false;">保存内容</button>
									<button class="btn btn-white" type="submit">取消</button>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>

	<input id="content" type="hidden" th:value="${info.content}"></input>
	<script th:src="@{/js/jquery.min.js}" src="js/jquery.min.js"></script>
	<script th:src="@{/js/bootstrap.min.js}" src="js/bootstrap.min.js"></script>
	<script th:src="@{/js/content.min.js}" src="js/content.min.js"></script>
	<script th:src="@{/js/plugins/iCheck/icheck.min.js}"
		src="js/plugins/iCheck/icheck.min.js"></script>
	<script th:src="@{/js/layui/layui.js}" src="js/layui/layui.js"></script>
	<script th:src="@{/js/wangEditor.min.js}" src="js/wangEditor.min.js"></script>
	<script>
	var contentE = $("#content").val();
	var E = window.wangEditor;
	var editor = new E('#editor');
	editor.customConfig.menus = [
		'head',  // 标题
	    'bold',  // 粗体
	    'fontSize',  // 字号
	    'foreColor',  // 文字颜色
	    'link',  // 插入链接
	    'image',  // 插入图片
	    'undo',  // 撤销
	    'redo'  // 重复
    ]
	//editor.customConfig.uploadImgShowBase64 = true;
	editor.customConfig.uploadImgServer = '/upload/uploadFile.do';
	editor.customConfig.uploadFileName = 'file'
	editor.customConfig.uploadImgHooks = {
		   
		    success: function (xhr, editor, result) {
		    	if(result.status == 10001){
		    		editor.txt.append('<img src="http://www.nbeasytest.com'+result.data[0].imgUrl+'" style="max-width:100%;">')
		    	}else{
		    		layer.msg(result.msg)
		    	}
		    	
		        // 图片上传并返回结果，图片插入成功之后触发
		        // xhr 是 XMLHttpRequst 对象，editor 是编辑器对象，result 是服务器端返回的结果
		    },
		    
		    error: function (xhr, editor) {
		    	layer.msg('图片上传出错！')
		        // 图片上传出错时触发
		        // xhr 是 XMLHttpRequst 对象，editor 是编辑器对象
		    },
		    timeout: function (xhr, editor) {
		    	layer.msg('图片上传超时！')
		        // 图片上传超时时触发
		        // xhr 是 XMLHttpRequst 对象，editor 是编辑器对象
		    },

		    // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式，可使用该配置
		    // （但是，服务器端返回的必须是一个 JSON 格式字符串！！！否则会报错）
		    customInsert: function (insertImg, result, editor) {
		        // 图片上传并返回结果，自定义插入图片的事件（而不是编辑器自动插入图片！！！）
		        // insertImg 是插入图片的函数，editor 是编辑器对象，result 是服务器端返回的结果

		        // 举例：假如上传图片成功后，服务器端返回的是 {url:'....'} 这种格式，即可这样插入图片：
		        var url = result.url
		        insertImg(url)

		        // result 必须是一个 JSON 格式字符串！！！否则报错
		    }
    }
		
	editor.create();
	editor.txt.html(contentE);
	
	layui.use('laydate', function(){
		  var laydate = layui.laydate;
		  laydate.render({ 
			  elem: '#hello'
			  ,type: 'datetime'
			});
		  
	});
	
	layui.use('upload',function() {
		var $ = layui.jquery, upload = layui.upload;
		//选完文件后不自动上传
		var uploadInst = upload
				.render({
					elem : '#test8',
					url : '/upload/uploadFile.do',
					auto : false,
					accept : 'file'

					//,multiple: true
					,
					bindAction : '#test9',
					before : function(obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
						$(".spiner-example").css('display',
								'inline-flex');
					},
					done : function(res) {
						$(".spiner-example").css('display',
								'none');
						console.log(res)
						if (res.status == 10001) {
							layer.msg("上传成功！");
							$("#filePath").val(res.data[0].imgUrl);
							$("#fileName").val(res.data[0].originalFileName);
							 var text = "<span class='layui-inline layui-upload-choose' title='"+res.data[0].imgUrl+"'>"
									+ res.data[0].originalFileName
									+ "</span>"; 
							$(".layui-upload span")
									.remove();
							$(".layui-upload").append(text);
						} else {
							layer.msg('请重试！')
						}

					},
					error : function(index, upload) {
						$(".spiner-example").css('display',
								'none');
						layer.msg('上传异常！');
					}
				});
		});
		function save() {
			var content = editor.txt.html(); 
			if($("#title").val() == ""){
				layer.msg('标题未填');
				return
			}
			if(typeid == "" || typeid == null){
				if($("#type").val() == ""){
					layer.msg('类型未选');
					return
				}
			}
			if($("#hello").val() == ""){
				layer.msg('时间未选');
				return
			}
			if(typeid == 3){
				if($("#filePath").val() == ""){
					layer.msg('未上传附件');
					return
				}
				if($("#reportNum").val() == ""){
					layer.msg('报告编码未填');
					return
				}
			}
			
			
			
			//content = content.replace(new RegExp("\n", "gm"), "</p><p>"); 
			$.ajax({
				url : "/information/addInformation.do",
				datatype : "json",
				data : {
					id : $("#infoId").val(),
					title : $("#title").val(),
					content : content,
					addTime : $("#hello").val(),
					file : $("#filePath").val(),
					fileName : $("#fileName").val(),
					companyName : $("#companyName").val(),
					amount : $("#amount").val(),
					reportNum : $("#reportNum").val(),
					reportCode : $("#reportCode").val(),
					type : $("#type").val()
				},
				type : 'POST',
				async : false,
				success : function(data) {
					if (data.status != 10001) {//
						layer.msg(data.msg);  
					} else {
						var typeIdgo = $("#type :selected").val();
						var typeName = $("#type :selected").text();
						layer.msg(data.msg, {
							  icon: 1,
							  time: 2000 
							}, function(){
								window.location.replace("/information/listView?typeId="+typeIdgo+"&typeName="+typeName)
							});  
					}
				}
			});
		}
		function GetQueryString(name){
		     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
		     var r = window.location.search.substr(1).match(reg);
		     if(r!=null)return  decodeURI(r[2]); return null;
		}
		var typeid="";
		//alert(GetQueryString(变量));
		$(document).ready(function() {
			typeid = GetQueryString("type");
			if( typeid == 3){
				$("#type3").show();
				$("#type4").hide();
				if(GetQueryString("infoId")){
					console.log("(^ - ^)");
				}else{
					$("#reportCode").val(randomString(8));
				}
				
			}
			else{
				$("#type3").hide();
				$("#type4").show();
			}
			if(typeid){
				$("#type").attr("disabled","disabled");
			}
		});
		$("#type").change(function(){
			var a = $("#type :selected").val();
			if( a == 3){
				$("#type3").show();
				$("#type4").hide();
				$("#reportCode").val(randomString(8));
			}
			else{
				$("#type3").hide();
				$("#type4").show();
			}
		});
		function randomString(len) {
			　　len = len || 32;
			　　var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
			　　var maxPos = $chars.length;
			　　var pwd = '';
			　　for (i = 0; i < len; i++) {
			　　　　pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
			　　}
			　　return pwd;
			}
	</script>
</body>


</html>
